package com.ay277.htqoa.shiro.shiroAuthorization;

import org.apache.shiro.subject.Subject;
import org.junit.Test;

import java.util.Arrays;


/**
 * 用户角色是否拥有测试
 * 用户角色是否拥有权限测试
 * Created by Me on 2016-12-25.
 */

public class shiroRoleTest {
    @Test
    public void  hasRole(){
        Subject currentUser= ShiroUtil.login("classpath:shiro_role.ini","admin","admin");
        System.out.println(currentUser.hasRole("role1")?"当前用户拥有role1角色":"当前用户未拥有role1角色");
        currentUser.logout();
    }
    @Test
    public void  hasAllRoles(){
        Subject currentUser= ShiroUtil.login("classpath:shiro_role.ini","java","java");
        boolean[] IsRoles= currentUser.hasRoles(Arrays.asList("role1","role2"));
        System.out.println(IsRoles[0]?"当前用户拥有role1角色":"当前用户未拥有role1角色");
        System.out.println(IsRoles[1]?"当前用户拥有role2角色":"当前用户未拥有role2角色");
        currentUser.logout();
    }
    @Test
    public void  hasRoles(){
        Subject currentUser= ShiroUtil.login("classpath:shiro_role.ini","java","java");
        System.out.println(currentUser.hasAllRoles(Arrays.asList("role1","role2"))?"当前用户拥有role1,role2角色":"当前用户不完全拥有role1，role2角色");
        currentUser.logout();
    }
    @Test
    public void  checkRole(){
        Subject currentUser= ShiroUtil.login("classpath:shiro_role.ini","java","java");
        currentUser.checkRole("role2");
        currentUser.logout();
    }
    @Test
    public void  checkRoles1(){
        Subject currentUser= ShiroUtil.login("classpath:shiro_role.ini","admin","admin");
        currentUser.checkRoles(Arrays.asList("role1","role2"));
        currentUser.logout();
    }
    @Test
    public void  checkRoles2(){
        Subject currentUser= ShiroUtil.login("classpath:shiro_role.ini","admin","admin");
        currentUser.checkRoles("role1","role2");
        currentUser.logout();
    }
    @Test
    //测试当前登录用户是否拥有权限
    public void  testIsPermitted(){
        Subject currentUser= ShiroUtil.login("classpath:shiro_permission.ini","java","java");
        System.out.println(currentUser.isPermitted("user:delete")?"当前用户拥有删除权限":"当前用户未拥有删除权限");
        currentUser.logout();
    }
    @Test
    public void  testIsPermittedList(){
        Subject currentUser= ShiroUtil.login("classpath:shiro_permission.ini","java","java");
        boolean[] IsPermistted=currentUser.isPermitted("user:delete","user:select","user:update","user:add");
        System.out.println(IsPermistted[0]?"当前用户拥有删除权限":"当前用户未拥有删除权限");
        System.out.println(IsPermistted[1]?"当前用户拥有查询权限":"当前用户未拥有查询权限");
        System.out.println(IsPermistted[2]?"当前用户拥有修改权限":"当前用户未拥有修改权限");
        System.out.println(IsPermistted[3]?"当前用户拥有添加权限":"当前用户未拥有添加权限");
        currentUser.logout();
    }
    @Test
    public void  testIsPermittedAll(){
        Subject currentUser= ShiroUtil.login("classpath:shiro_permission.ini","java","java");
        System.out.println(currentUser.hasAllRoles(Arrays.asList("user:select","user:delete","user:update","user:add"))?"当前用户拥有全部权限":"当前用户不完全所有权限");
        currentUser.logout();
    }
    @Test
    public void  testCkeckPermitted(){
        Subject currentUser= ShiroUtil.login("classpath:shiro_permission.ini","admin","admin");
        //currentUser.checkPermission("user:update");//"user:delete","user:update","user:add"
        currentUser.checkPermissions("user:update","user:delete","user:update","user:add");
        currentUser.logout();
    }
}
